package com.wmh.baseservice.skd.controller;

import com.wmh.baseservice.common.service.TokenService;
import com.wmh.baseservice.common.utils.BankCardCheck;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.skd.pojo.SkdBankCard;
import com.wmh.baseservice.skd.pojo.vo.AddBankCardReqVO;
import com.wmh.baseservice.skd.pojo.vo.BankCardRespVO;
import com.wmh.baseservice.skd.service.SkdBankCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Objects;

@Api(tags = "银行卡相关接口")
@RestController
@RequestMapping(value = "bank-card", produces = "text/plain;charset=utf-8")
@Slf4j
@Validated
public class BankCardController {
    @Resource
    private TokenService tokenService;
    @Resource
    private SkdBankCardService skdBankCardService;

    /**
     * #0074 绑定银行卡
     * @return 返回是否绑定成功信息
     */
    @ApiOperation("绑定银行卡")
    @PostMapping
    public String bindBankCard(@Valid AddBankCardReqVO reqVO) {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        // 通过Luhn算法校验是否为有效银行卡号
        if(!BankCardCheck.checkBankCard(reqVO.getBankCardNumber())) {
            return ReturnBody.error("无效的银行卡, 请检查后重试!");
        }
        if (!Objects.isNull(skdBankCardService.getByUserId(usId))) {
            return ReturnBody.error("该用户已绑定银行卡!");
        }
        SkdBankCard skdBankCard = new SkdBankCard();
        skdBankCard.setUserId(usId)
                .setCardholder(reqVO.getCardholder())
                .setBankCardNumber(reqVO.getBankCardNumber())
                .setBankOfDeposit(reqVO.getBankOfDeposit())
                .setAccountOpeningBranch(reqVO.getAccountOpeningBranch())
                .setPhoneNumber(reqVO.getPhoneNumber());
        skdBankCardService.save(skdBankCard);
        return ReturnBody.success("绑定成功!");
    }

    /**
     * #0075 删除银行卡
     * @return 返回是否删除成功信息
     */
    @ApiOperation("删除银行卡")
    @DeleteMapping
    public String deleteBankCard(@NotBlank(message = "未提交持卡人姓名") String cardholder, @NotBlank(message = "未提交银行卡号") String bankCardNumber) {
        Long bankCardId = skdBankCardService.getBankCardIdByCardholderAndBankCardNumber(cardholder, bankCardNumber);
        if (Objects.isNull(bankCardId)) {
            return ReturnBody.error("该银行卡不存在!");
        }
        skdBankCardService.removeById(bankCardId);
        return ReturnBody.success("删除成功!");
    }

    /**
     * #0076 查看银行卡
     * @return 返回绑定的银行卡信息
     */
    @ApiOperation("查看银行卡")
    @GetMapping
    public String viewBankCard() {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        SkdBankCard skdBankCard = skdBankCardService.getByUserId(usId);
        if (Objects.isNull(skdBankCard)) {
            return ReturnBody.error("暂无银行卡信息");
        }
        BankCardRespVO respVO = new BankCardRespVO();
        respVO.setCardholder(skdBankCard.getCardholder())
                .setBankCardNumber(skdBankCard.getBankCardNumber())
                .setBankOfDeposit(skdBankCard.getBankOfDeposit())
                .setAccountOpeningBranch(skdBankCard.getAccountOpeningBranch())
                .setPhoneNumber(skdBankCard.getPhoneNumber())
                .setStatus(skdBankCard.getStatus());
        return ReturnBody.success(respVO);
    }

}
